//
//  EPCBaseTableController.swift
//  Weibo
//
//  Created by 吴林峰 on 01/04/2017.
//  Copyright © 2017 MyGit. All rights reserved.
//

import UIKit

// MARK: - baseController
class EPCBaseTableController: UIViewController {
    
    /// visitorInfo
    var visitorInfoDict: [String: String]?
    
    var tableView: UITableView?
    
    var refreshControl: UIRefreshControl?
    
    lazy var navigationBar = UINavigationBar.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.cz_screenWidth(), height: 64))
    
    lazy var navItem = UINavigationItem()
    
    
    override func viewDidLoad() {
        
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        setupUI()
        
        EPCNetworking.shareManager.userLogon ? loadData() : ()
        
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(loginStateChanged),
            name: NSNotification.Name(EPCUserLoginSuccessNotification),
            object: nil)
    
    }
    
    
    override var title: String? {
        
        didSet {
            navItem.title = title
        }
    }
    
    // load Datas
    func loadData() {
        refreshControl?.endRefreshing()
    }
    
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}

// MARK: - action
extension EPCBaseTableController {
    
    func register() {
        
    }
    
    func login() {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue:EPCUserShouldLoginNotification), object: nil)
    }
    
    func loginStateChanged() {
        
        navItem.leftBarButtonItem = nil
        navItem.rightBarButtonItem = nil
        
        view = nil
        
        // remove observer
        NotificationCenter.default.removeObserver(self)
    }
}

// MARK: - setup
extension EPCBaseTableController {

    fileprivate func setupUI() {
        
        // when hidden navgitionbar, up 20 pixel point
        automaticallyAdjustsScrollViewInsets = false
        
        setupNavBar()
        
        EPCNetworking.shareManager.userLogon ? setupTableview() : setupVisitor()
    }
    
    func setupTableview() {
        
        tableView = UITableView.init(frame: view.bounds, style: .plain)
        
        view.insertSubview(tableView!, belowSubview:navigationBar)
        
        tableView?.delegate = self
        tableView?.dataSource = self
        
        tableView?.contentInset = UIEdgeInsets.init(top:navigationBar.bounds.height,
                                                   left:0,
                                                   bottom:self.tabBarController?.tabBar.bounds.height ?? 49,
                                                   right:0)
        
        tableView?.scrollIndicatorInsets = tableView!.contentInset
        
        // refreshControl
        refreshControl = UIRefreshControl()
        tableView?.addSubview(refreshControl!)
        refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
    }
    
    private func setupVisitor() {
        let visitorView = EPCVisitorView.init(frame: view.bounds)
        view.insertSubview(visitorView, belowSubview: navigationBar)
        
        visitorView.visitorInfo = visitorInfoDict
        
        visitorView.registerButton.addTarget(self, action: #selector(register), for: .touchUpInside)
        visitorView.loginButton.addTarget(self, action: #selector(login), for: .touchUpInside)
        
        navItem.leftBarButtonItem = UIBarButtonItem.init(title: "注册", style: .plain, target: self, action: #selector(register))
        navItem.rightBarButtonItem = UIBarButtonItem.init(title:"登录", style: .plain, target: self, action: #selector(login))
    }
    
    private func setupNavBar() {
        self.view.backgroundColor = UIColor.white
        self.view.addSubview(navigationBar)
        navigationBar.items = [navItem]
        navigationBar.barTintColor = UIColor.cz_color(withHex: 0xf6f6f6)
        navigationBar.tintColor = UIColor.orange
    }
}

// MARK: - UITableViewDelegate, UITableViewDataSource
extension EPCBaseTableController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
}
